{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# K-Nearest Neighbors Classification Demo\n",
    "\n",
    "K-nearest neighbors classification uses the labels of neighborhoods around data samples to classify unseen data samples. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The model can take array-like objects, either in host as NumPy arrays or in device (as Numba or cuda_array_interface-compliant), as well as cuDF DataFrames as the input. \n",
    "\n",
    "For information on converting your dataset to cuDF format, refer to the cuDF documentation: https://docs.rapids.ai/api/cudf/stable\n",
    "\n",
    "For additional information on cuML's Nearest Neighbors implementation: https://rapidsai.github.io/projects/cuml/en/stable/api.html#nearest-neighbors-classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.datasets import make_blobs\n",
    "\n",
    "import pandas as pd\n",
    "import cudf as gd\n",
    "\n",
    "from sklearn.neighbors import KNeighborsClassifier as skKNC\n",
    "from cuml.neighbors import KNeighborsClassifier as cumlKNC"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_samples = 2**17\n",
    "n_features = 40\n",
    "\n",
    "n_query = 5000\n",
    "\n",
    "n_neighbors = 4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate Data\n",
    "\n",
    "### Host"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "X_host_train, y_host_train = make_blobs(\n",
    "   n_samples=n_samples, n_features=n_features, centers=5, random_state=0)\n",
    "\n",
    "X_host_train = pd.DataFrame(X_host_train)\n",
    "y_host_train = pd.DataFrame(y_host_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "X_host_test, y_host_test = make_blobs(\n",
    "   n_samples=n_query, n_features=n_features, centers=5, random_state=0)\n",
    "\n",
    "X_host_test = pd.DataFrame(X_host_test)\n",
    "y_host_test = pd.DataFrame(y_host_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_device_train = gd.DataFrame.from_pandas(X_host_train)\n",
    "y_device_train = gd.DataFrame.from_pandas(y_host_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_device_test = gd.DataFrame.from_pandas(X_host_test)\n",
    "y_device_test = gd.DataFrame.from_pandas(y_host_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scikit-learn Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "knn_sk = skKNC(algorithm=\"brute\", n_neighbors=n_neighbors, n_jobs=-1)\n",
    "knn_sk.fit(X_host_train, y_host_train)\n",
    "\n",
    "sk_result = knn_sk.predict(X_host_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## cuML Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "knn_cuml = cumlKNC(n_neighbors=n_neighbors)\n",
    "knn_cuml.fit(X_device_train, y_device_train)\n",
    "\n",
    "cuml_result = knn_cuml.predict(X_device_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare Results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "passed = np.array_equal(np.asarray(cuml_result.as_gpu_matrix())[:,0], sk_result)\n",
    "print('compare knn: cuml vs sklearn classes %s'%('equal'if passed else 'NOT equal'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
